/**
* add to body onLoad="autoindex('autoindex')" where  "autoindex" is the id of the object where index will be created
*/
indexList = new Array();
function autoindex( elementId ) {
	//var element = document.getElementById(elementId);
	var bodyTag = document.getElementsByTagName("body");
	var list = searchHeadlines( bodyTag[0] );
	
	element = document.getElementById(elementId);
	
	newContents = "";
	newContents = "<ol class=\"autoIndex\">";
	var lines = 0;
	
	for( var i=0; i<indexList.length; i++ ) {
		if( indexList[i].tagName == "H1" ) {
			if( lines != 0 ) newContents += "</ol>";
			newContents += "<li><a href=\"#link_"+i+"\">"+indexList[i].innerHTML+" </a></li><ol>";
		} else {
			newContents += "<li><a href=\"#link_"+i+"\">"+indexList[i].innerHTML+" </a></li>";
		}
		indexList[i].innerHTML = "<a name=\"link_"+i+"\">"+indexList[i].innerHTML+"</a> <a href=\"#link_2\"></a>";
		lines++;
	}
	
	element.innerHTML = newContents+"</ol>";
}

function searchHeadlines( element ) {

	for( var i=0; i<element.childNodes.length; i++) {
		//if( i<2) alert(element.childNodes[i].tagName);
		if( element.childNodes[i].tagName == "H1") {
			indexList.push( element.childNodes[i]);
		} else if( element.childNodes[i].tagName == "H2") {
			indexList.push( element.childNodes[i]);
		} else {
			searchHeadlines( element.childNodes[i] );
		}
	}
}